//
//  EntryViewController.m
//  ReciteBook
//
//  Created by xuziyuan on 13-12-22.
//  Copyright (c) 2013年 student. All rights reserved.
//

#import "EntryViewController.h"
#import "User.h"
#import "HomeViewController.h"

@interface EntryViewController ()

@end

@implementation EntryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
   
}

- (IBAction)backgroundTop:(id)sender
{
    [self.userNameTextField resignFirstResponder];
    [self.passwordsTextField resignFirstResponder];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.myAppDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
 
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
    self.navigationItem.rightBarButtonItem=rightBtn;
    [self queryUser];

}
//登录完成发送消息
- (void)finish
{
    int flag=0;
    for (User *newuser in self.listUser) {
        if ([newuser.emailAdress isEqualToString:self.userNameTextField.text]&&[newuser.password isEqualToString:self.passwordsTextField.text]) {
            NSLog(@"成功登录");
            NSLog(@"iduser=%@,name=%@,email=%@,password=%@",newuser.idUser,newuser.nameUser,newuser.emailAdress,newuser.password);
            HomeViewController *vc=(HomeViewController *)self.delegate;
            vc.name=newuser.nameUser;
            
            flag=1;
            break;
        }
    }
    if (flag==0) {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"账号不存在或密码不正确" message:@"请重新确认" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
    }else if (flag==1){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)queryUser
{
    NSEntityDescription *entityUser=[NSEntityDescription entityForName:@"User" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
    NSFetchRequest *request=[[NSFetchRequest alloc] init];
    
    [request setEntity:entityUser];
    self.listUser=[[self.myAppDelegate.managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
